< previous page page_161 next page >

Page 161
to put debugging code in your application, but when you create a final compile, you dont want the debugging code to be there. Also, maybe you want to take out the code for certain features if youre creating a demo version of your product.
Examples Filename: Conditional.frm
#If . . . Then . . . #Else . . . #End If
To create a compiler directive, you use the # symbol in front of the If statement. This tells the compiler that the #If statement is for it, not for runtime. An example might be to display different languages to a user depending on the compiled version of your code. Heres an example:
9f31265bb40b592b1929e18a47ab28e5.gif
Private Sub cmdConditional_Click()
   #If cconLANGUAGE = 1 Then
      MsgBox "Good Morning, Mr. Gates"
   #Else
      MsgBox "Guten Morgen, Herr Gates"
   #End If
End Sub
When you declare a compiler constant called cconLANGUAGE and set that value to 1, the English edition of the MsgBox statement is included in the EXE file. If the value is set to anything else, the German version is included.
Unlike a normal If statement, the #If statement actually removes the code before it compiles. This leads to reduced EXE size and also prevents compilation errors if the environment does not match the code.
Although not mentioned in the Visual Basic manual, logical operators can be used in the #If statement. For example, the following code is perfectly legal:
9f31265bb40b592b1929e18a47ab28e5.gif
#If cconDEBUGCODE = 1 Or cconDEBUGCODE = 2 Then
   MsgBox "Debugging"
#End If
You could also use the conditional compilation feature to turn off your generic error-handling code or maybe call a special error-trapping routine. Youll learn more about error handling in Chapter 17. Heres an example:
9f31265bb40b592b1929e18a47ab28e5.gif
#If cconERRORSPECIAL = 0 Then
   On Error Goto Load_EH
#Else
   On Error Goto Special_EH
#End If

 
< previous page page_161 next page >

If you like this book, buy it!